<template>
  <el-container class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="main-header">
      <div class="header-content">
        <div class="logo">
          <el-icon class="logo-icon"><Timer /></el-icon>
          <span>鎏一涧茶咖馆</span>
        </div>
        <div class="user-info">
          <el-dropdown trigger="click">
            <div class="user-dropdown-link">
              <el-icon><User /></el-icon>
              <span>{{ username }}</span>
              <el-icon><CaretBottom /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">
                  <el-icon><User /></el-icon>
                  <span>个人资料</span>
                </el-dropdown-item>
                <el-dropdown-item @click="logout">
                  <el-icon><SwitchButton /></el-icon>
                  <span>重新登录</span>
                </el-dropdown-item>
                <el-dropdown-item @click="exitSystem">
                  <el-icon><Close /></el-icon>
                  <span>退出系统</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
        <el-menu
          :collapse="isCollapse"
          :router="true"
          class="sidebar-menu"
        >
          <el-menu-item index="/dashboard">
            <el-icon><Monitor /></el-icon>
            <span>工作台</span>
          </el-menu-item>
          <el-menu-item index="/rooms">
            <el-icon><House /></el-icon>
            <span>房间管理</span>
          </el-menu-item>
          <el-menu-item index="/products">
            <el-icon><Food /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="/report">
            <el-icon><TrendCharts /></el-icon>
            <span>营业报表</span>
          </el-menu-item>
          <el-menu-item index="history">
            <el-icon><Document /></el-icon>
            <span>结算记录</span>
          </el-menu-item>
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主要内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { Monitor, User, Setting, House, Food, Document, Timer, CaretBottom, SwitchButton, TrendCharts, Close } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'MainLayout',
  components: {
    Monitor,
    User,
    Setting,
    House,
    Food,
    Document,
    Timer,
    CaretBottom,
    SwitchButton,
    TrendCharts,
    Close
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const isCollapse = ref(false)

    const username = computed(() => store.state.user.username)

    const goToProfile = () => {
      router.push('/profile')
    }

    const logout = () => {
      // 清除 token
      localStorage.removeItem('token')
      store.commit('user/setToken', null)
      
      // 提示用户
      ElMessage({
        type: 'success',
        message: '已退出登录'
      })

      // 跳转到登录页
      router.push('/login')
    }

    const exitSystem = () => {
      ElMessage({
        type: 'success',
        message: '正在退出系统...'
      })
      // 使用 electron API 退出系统
      window.electronAPI.ipcRenderer.send('close-app')
    }

    return {
      isCollapse,
      username,
      goToProfile,
      logout,
      exitSystem
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.main-header {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  color: #fff;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 500;
}

.logo-icon {
  font-size: 24px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown-link {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-dropdown-link:hover {
  background: rgba(255,255,255,.1);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 4px;
}

.sidebar {
  transition: width 0.3s;
  background-color: #545c64;
}

.sidebar-menu {
  height: 100%;
}

@media (max-width: 768px) {
  .el-aside {
    position: fixed;
    z-index: 1000;
    height: 100vh;
  }
}
</style> 